package Servlets.yz;

import Bean.supplier;
import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.LocalDateTime;

/*
* 用于供应商的更改功能
* */
@WebServlet(name="SupplyChangeServlet",urlPatterns = "/SupplyChangeServlet")
public class SupplyChangeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");

        if("updateBefore".equals(method)){//更新之前，获取原供应商信息
            String id = req.getParameter("id");
            String sql = "select * from supplier where id=?";
            supplier supplier = GokJDBCUtil2.queryForObject(sql, supplier.class, id);
            req.setAttribute("supplier",supplier);
            req.getRequestDispatcher("pages/SupplyUpdate.jsp").forward(req,resp);
        }
        else if("update".equals(method)){//结合更新之间的数据，进行相应更新
            String supplierID = req.getParameter("supplierID");
            String suppliercontractid = req.getParameter("suppliercontractid");
            String supplierenterprise = req.getParameter("supplierenterprise");
            String supplyName = req.getParameter("supplyName");
            String supplierphone = req.getParameter("supplierphone");
            String supplieraddress = req.getParameter("supplieraddress");
            String supplieremail = req.getParameter("supplieremail");
            LocalDateTime updateTime = LocalDateTime.now();
            String sql = "update  supplier set name=?,contactperson=?,phonenumber=?,email=?,address=?,contractid=?,updatetime=? where id=?";
            GokJDBCUtil2.update(sql,supplierenterprise,supplyName,supplierphone,supplieremail,supplieraddress,suppliercontractid,updateTime,supplierID);
            resp.sendRedirect(req.getContextPath()+"/pages/supplyUpSuccess.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
